package cmj.cloudmusic.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.activity.MainActivity;
import cmj.cloudmusic.adapter.MainFragmentAdapter;
import cmj.cloudmusic.fragment.Discover.DiscoverFragment;
import cmj.cloudmusic.fragment.Friends.FriendsFragment;

/**
 * Created by 王虎 on 2016/12/12.
 */
public class Main_Fragment extends Fragment implements View.OnClickListener {
    private View view;
    //主页面 控制栏
    private ImageButton menu;
    private ImageButton serch;
    private RadioGroup radioGroup;
    private RadioButton discover;
    private RadioButton music;
    private RadioButton friends;
    //主页vp
    private ViewPager viewPager;
    private List<Fragment> vpFragments;
    private FragmentPagerAdapter adapter;

    private boolean state;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.main_fragment,null);
        initMainView();
        bindEvent();
        return view;
    }
    // 初始化控件
    private void initMainView(){
        menu=(ImageButton)view.findViewById(R.id.menu);
        serch= (ImageButton) view.findViewById(R.id.search);

        radioGroup= (RadioGroup)view. findViewById(R.id.mainrbts);
        radioGroup.check(R.id.discover);
        discover= (RadioButton) view.findViewById(R.id.discover);
        music= (RadioButton) view.findViewById(R.id.music);
        friends= (RadioButton) view.findViewById(R.id.friends);


        viewPager= (ViewPager) view.findViewById(R.id.mainviewpager);

        initViewPager();

    }
    //初始化 主viewpager
    private void initViewPager(){
        DiscoverFragment discoverFragment=new DiscoverFragment();
        MusicFragment musicFragment=new MusicFragment();
        FriendsFragment friendsFragment=new FriendsFragment();

        vpFragments=new ArrayList<>();
        vpFragments.add(discoverFragment);
        vpFragments.add(musicFragment);
        vpFragments.add(friendsFragment);

        adapter=new MainFragmentAdapter(getChildFragmentManager(),vpFragments);

        viewPager.setAdapter(adapter);
    }
    //绑定事件
    private void bindEvent(){
        menu.setOnClickListener(this);
        serch.setOnClickListener(this);
        discover.setOnClickListener(this);
        music.setOnClickListener(this);
        friends.setOnClickListener(this);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (!state){
                    switch (checkedId){
                        case R.id.discover:viewPager.setCurrentItem(0);break;
                        case R.id.music:viewPager.setCurrentItem(1);break;
                        case R.id.friends:viewPager.setCurrentItem(2);break;
                    }
                }

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                state = true;
                switch (position) {
                    case 0:
                        radioGroup.check(R.id.discover);
                        break;
                    case 1:
                        radioGroup.check(R.id.music);
                        break;
                    case 2:
                        radioGroup.check(R.id.friends);
                        break;
                }
                state = false;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.menu:
                ((MainActivity)getActivity()).drawerLayout.openDrawer(((MainActivity)getActivity()).left_drawer);
                break;
            case R.id.search:

                break;
            case R.id.discover:

                break;
            case R.id.music:

                break;
            case R.id.friends:

                break;


        }
    }
}
